package honest.playmusic.util;

import honest.playmusic.bean.LrcInfo;
import honest.playmusic.bean.Music;
import honest.playmusic.bean.OnlineLrc;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

/**
 * 歌词获取,通过歌词迷
 * @author 李跃东
 * @mail androiddevelop@qq.com
 * @date 2013-11-8
 */
public class OnlineLrcManager {
	private static List<String> lrcList=new ArrayList<String>();
	private static int lrcNum=0; //歌词不正确可以进行更换
	private static Music music; 

	/**
	 * 获取歌词所有地址
	 * @param title 歌曲名
	 * @param singer 歌手
	 * @throws IOException 
	 * @throws MalformedURLException 
	 */
	private List<String> getMusicLrc(String title,String singer) throws MalformedURLException, IOException{

		List<String> address=new ArrayList<String>();
		OnlineLrc onlineLrc=getOnlineLrc(title, singer);
		List<LrcInfo> lrc=onlineLrc.getResult();
		for(int i=0;i<lrc.size();i++) {
			address.add(lrc.get(i).getLrc());
		 }
		return address;
	}

	/**
	 * 网上得到歌词包
	 * @param title 歌曲名
	 * @param singer 歌手
	 * @throws IOException 
	 * @throws MalformedURLException 
	 */
	private OnlineLrc getOnlineLrc(String title,String singer) throws MalformedURLException, IOException{
		String musicAddress="http://geci.me/api/lyric/"+title+"/"+singer;
		String res=getHttpStream(musicAddress);
		OnlineLrc lrc=new OnlineLrc();
		Gson gson=new Gson();
		lrc=gson.fromJson(res, new TypeToken<OnlineLrc>(){}.getType());
		return lrc;
	}

	/**
	 * 获取在线歌词并且保存到文件
	 * 方便统一管理
	 * @param music 需要搜索的歌曲
	 * @return 将单个歌词内容取出写入文件中
	 */
	public boolean getLrcAndWriteFile(Music music){
		String lrcPath=music.getMusicAbsolutePath();
		lrcPath=lrcPath.substring(0, lrcPath.length()-3)+"lrc";
		final String title=music.getMusicName();
		final String singer=music.getMusicAuthor();

		if(OnlineLrcManager.music==null||music!=OnlineLrcManager.music){
			lrcList.clear();
			OnlineLrcManager.music=music;
		}

		String res=null;
		try {
			if(lrcList.size()==0){
				lrcNum=0;
				lrcList=getMusicLrc(title,singer);
			}
		} catch (IOException e) {
			e.printStackTrace();
			return false;
		}

		if(lrcNum!=lrcList.size()){
			try {
				res=getHttpStream(lrcList.get(lrcNum++));
			} catch (IOException e) {
				e.printStackTrace();
				return false;
			}
		}

		FileManager fileManager=new FileManager();
		try {
			fileManager.writeFile(lrcPath, res);
		} catch (IOException e) {
			return false;
		}
		return true;
	}


	/**
	 * 网络操作
	 * @param url
	 * @return 网络流对应字符串
	 * @throws IOException
	 */
	private String getHttpStream(String url) throws IOException{
		URLConnection connection=new URL(url).openConnection();
		BufferedReader in=new BufferedReader(new InputStreamReader(connection.getInputStream()));
		StringBuffer res=new StringBuffer("");
		String tmp;
		while((tmp=in.readLine())!=null){
			res.append(tmp);
			res.append("\n");
		}
		tmp=res.toString();
		tmp=tmp.substring(0, tmp.length()-1);
		in.close();
		return tmp;
	}

}
